package com.sfpay.coreplatform.order.service.composite.impl;

import java.security.NoSuchAlgorithmException;

import org.springframework.stereotype.Service;

 
import com.sfpay.coreplatform.account.common.enums.AccountActive;
import com.sfpay.coreplatform.account.common.enums.AccountState;
import com.sfpay.coreplatform.account.common.enums.FreezeState;
import com.sfpay.coreplatform.account.valueobject.tmo.AccountVO;
import com.sfpay.coreplatform.order.common.OrderResponseCode;
import com.sfpay.coreplatform.order.service.composite.common.PayContext;
import com.sfpay.coreplatform.order.service.exception.OrderServiceException; 
import com.sfpay.coreplatform.order.valueobject.dto.PayRequest;
import com.sfpay.coreplatform.order.valueobject.dto.Response;
import com.sfpay.framework.common.util.Md5Utils;
@Service
public class CheckAccHandler extends AbstractHandler {

	@Override
	public Response Handler(PayRequest request, PayContext payContext) {
		Response response = new Response();
		response.setReturnCode(OrderResponseCode.ORDER_RESPONSE_ACC_NOT_NORMAL);

		AccountVO accountVo = null;
		try {
			accountVo = this.getAccountService().findAccountInfoByAccountNo(request
					.getPayerAcctNo());
		} catch (OrderServiceException e) {

			response.setReturnMsg("methode:checkAccHandel accno:"
					+ request.getPayerAcctNo() + " query acc exception");
			return response;
		}
		if (accountVo == null) {

			response.setReturnMsg("methode:checkAccHandel accno:"
					+ request.getPayerAcctNo() + " acc not exist");
			return response;
		}

		if (request.isAccPasword()) {// 判断是否要秘密
			String password = request.getPayerPassword();
			try {
				password = Md5Utils.encryptMD5(password);
			} catch (NoSuchAlgorithmException e) {

				response.setReturnMsg("methode:checkAccHandel accno:"
						+ request.getPayerAcctNo()
						+ " password encipherment exception ");
				return response;
			}

			request.setPayerPassword(password);// 把参数明文密码转为已经加密

			if (!password.equals(accountVo.getAccountPasswd())) {// 判断密码

				response.setReturnMsg("methode:checkAccHandel accno:"
						+ request.getPayerAcctNo() + " password is not correct");
				response.setReturnCode(OrderResponseCode.ORDER_COMMON_PARAM_ERR_PASSWORD);
				return response;
			}
		}
		if (AccountActive.UN_ACTIVE == accountVo.getAccountActive()) {// 激活

			response.setReturnMsg("methode:checkAccHandel accno:"
					+ request.getPayerAcctNo() + " acc is not Active");
			return response;

		}
		if (AccountState.CANCEL == accountVo.getAccountState() || AccountState.OUT_FORBIDEN == accountVo.getAccountState()) {// 状态

			response.setReturnMsg("methode:checkAccHandel accno:"
					+ request.getPayerAcctNo() + " acc  is not normal");
			return response;
		}
		if (FreezeState.FREEZE == accountVo.getFreezeState()) {// 冻结

			response.setReturnMsg("methode:checkAccHandel accno:"
					+ request.getPayerAcctNo() + " acc is freeze");
			return response;
		}		 

		response.setReturnCode(OrderResponseCode.ORDER_RESPONSE_SUCESS);
		return response;
	}

	@Override
	public String getHandlerName() {
		 
		return "CheckAccHandler";
	}

}
